//

#import "AM_MainViewController.h"
#import "AM_HomeTableViewCell.h"
#import "AM_HomeAddViewController.h"
#import "AM_HomeDetailViewController.h"


@interface AM_MainViewController ()<UITableViewDelegate,UITableViewDataSource>
/** <#Description#>*/
@property (nonatomic, strong) UITableView *tableView;
/** <#Description#>*/
@property (nonatomic, strong) NSMutableArray *dataArray;
/** <#Description#>*/
@property (nonatomic, strong) AM_CoreDataView *fmdb;
/** <#Description#>*/
@property (nonatomic, assign) BOOL  isGo;

@end

@implementation AM_MainViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _fmdb = [[AM_CoreDataView alloc]initWithCoreData:@"HomeData" modelName:fmdt success:^{} fail:^(NSError * _Nullable error) {}];
    [self getDatas];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"Memorandum";
    
    // 弱引用
    __weak typeof(self) weakSelf = self;
    [self setupLeftAndwqwRightSetting:@"Add" block:^{
        //
        AM_HomeAddViewController *vc = [[AM_HomeAddViewController alloc]init];
        UINavigationController * navigation = [[UINavigationController alloc]initWithRootViewController:vc];
        [weakSelf presentViewController:navigation animated:YES completion:nil];
        
    }];
    //
    [self.view addSubview:self.tableView];
}


//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    AM_HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[AM_HomeTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArray[indexPath.section];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRelativeHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //
    AM_HomeDetailViewController *vc = [[AM_HomeDetailViewController alloc]init];
    vc.model = _dataArray[indexPath.section];
    [self.navigationController pushViewController:vc animated:YES];
}
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        HomeData *model = _dataArray[indexPath.section];
        //        //
        //        NSMutableDictionary *parameter = [NSMutableDictionary new];
        //        [parameter setObject:@"" forKey:@"img"];
        //        [parameter setObject:@"777" forKey:@"name"];
        //        [parameter setObject:@"777" forKey:@"account"];
        //        [parameter setObject:@"" forKey:@"password"];
        //        // 弱引用
        //        __weak typeof(self) weakSelf = self;
        //        [_fmdb am_updateEntity:parameter key:[NSString stringWithFormat:@"id=%@",model.id] success:^{
        //            [weakSelf getDatas];
        //        } fail:^(NSError * _Nullable error) {
        //
        //        }];
        HomeAddViewController *vc = [[HomeAddViewController alloc]init];
        UINavigationController * navigation = [[UINavigationController alloc]initWithRootViewController:vc];
        vc.model = model;
        [self presentViewController:navigation animated:YES completion:nil];
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"Edit";
}
//*/


//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight  = kRelativeHeight(70);
        _tableView.separatorColor = [UIColor whiteColor];
        _tableView.backgroundColor = RGB(245, 246, 246);
    }
    return _tableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }return _dataArray;
}

#pragma mark ----------------- 获取数据 ----------------
- (void)getDatas {
    //
    __weak typeof(self) weakSelf = self;
    [_fmdb am_readEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results)return ;
        self.dataArray = [results copy];
        [self.tableView reloadData];
        
        if (self.dataArray.count <= 0 && !weakSelf.isGo) {
            weakSelf.isGo = YES;
            AM_HomeAddViewController *vc = [[AM_HomeAddViewController alloc]init];
            UINavigationController * navigation = [[UINavigationController alloc]initWithRootViewController:vc];
            [weakSelf presentViewController:navigation animated:YES completion:nil];
        }
    } fail:^(NSError * _Nullable error) {
        
    }];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (BOOL)am_isShowLeftSetting {
    return NO;
}

@end
